package com.ymatou.iapi.tradingsettlement.testcase;

import java.math.BigDecimal;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.tradingsettlement.parameter.ManageFeeStatisticsReq;
import com.ymatou.iapi.tradingsettlement.service.ManageFeeStatisticsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.hunter.HunterSettlementWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

@Ignore
public class Ts_ManageFeeStatistics {
	
	private static ManageFeeStatisticsReq manageFeeStatisticsReq;
	private static ManageFeeStatisticsCall manageFeeStatisticsCall;
	
	HunterSettlementWrapper hunterSettlementWrapper = new HunterSettlementWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("推手管理服务费统计");
	}

	@Before
	public void caseUp() {
		manageFeeStatisticsReq = new ManageFeeStatisticsReq();
		manageFeeStatisticsCall = new ManageFeeStatisticsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(manageFeeStatisticsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws JSONException {

		JSONArray expectMonthIncomeVoList = manageFeeStatisticsCall.getData()
				.getJSONArray("monthIncomeVoList");

		Map<Integer, BigDecimal> actualTotalIncomeMap = hunterSettlementWrapper
				.totalManageFeeIncomeMonthStatistics(
						manageFeeStatisticsReq.getHunterId(),
						manageFeeStatisticsReq.getYear());

		Map<Integer, BigDecimal> actualSettledIncomeMap = hunterSettlementWrapper
				.settledManageFeeIncomeMonthStatistics(
						manageFeeStatisticsReq.getHunterId(),
						manageFeeStatisticsReq.getYear());

		BigDecimal actualTotalAmount = hunterSettlementWrapper.totalManageFeeAmountYear(
				manageFeeStatisticsReq.getHunterId(),
				manageFeeStatisticsReq.getYear());

		for (int i = 0; i < expectMonthIncomeVoList.length(); i++) {

			JSONObject expectObject = expectMonthIncomeVoList.getJSONObject(i);

			int expectMonth = expectObject.getInt("month");
			String expectTotalIncome = new BigDecimal(
					expectObject.optString("totalIncome")).stripTrailingZeros()
					.toPlainString();
			String expectSettledIncome = new BigDecimal(
					expectObject.optString("settledIncome"))
					.stripTrailingZeros().toPlainString();

			DataUtil.verifyEquals(expectMonth, i + 1, "验证month");
			DataUtil.verifyEquals(expectTotalIncome,
					actualTotalIncomeMap.get(i + 1).stripTrailingZeros()
							.toPlainString(), "验证month");
			DataUtil.verifyEquals(expectSettledIncome, actualSettledIncomeMap
					.get(i + 1).stripTrailingZeros().toPlainString(), "验证month");
		}

		DataUtil.verifyEquals(
				new BigDecimal(manageFeeStatisticsCall.getData().optString(
						"totalAmount")).stripTrailingZeros().toPlainString(),
				actualTotalAmount.stripTrailingZeros().toPlainString(),
				"验证totalAmount");
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeStatistics_001() {
		Logger.start(true, "推手管理服务费统计");
		try {
			manageFeeStatisticsReq.setHunterId(3025);
			manageFeeStatisticsReq.setYear(YMTDateUtil
					.getCurrentYearMonthDate("year"));

			manageFeeStatisticsCall.setData(manageFeeStatisticsReq);
			manageFeeStatisticsCall.callService();
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeStatistics_002() {
		Logger.start(true, "推手管理服务费统计（没有管理费，只有佣金）");
		try {
			manageFeeStatisticsReq.setHunterId(1022);
			manageFeeStatisticsReq.setYear(YMTDateUtil
					.getCurrentYearMonthDate("year"));

			manageFeeStatisticsCall.setData(manageFeeStatisticsReq);
			manageFeeStatisticsCall.callService();
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionStatistics_003() {
		
		Logger.start(true, "推手id为空");
		
		try {

			manageFeeStatisticsReq.setHunterId(0);
			manageFeeStatisticsReq.setYear(2018);

			manageFeeStatisticsCall.setData(manageFeeStatisticsReq);
			manageFeeStatisticsCall.callService();

			DataUtil.verifyEquals(400, manageFeeStatisticsCall.getJSONData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(100, manageFeeStatisticsCall.getJSONData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("hunterId:推手id不能为空", manageFeeStatisticsCall
					.getJSONData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, manageFeeStatisticsCall.getData()
					.getBoolean("success"), "验证success");
			DataUtil.verifyEquals("ILLEGAL_ARGUMENT", manageFeeStatisticsCall
					.getData().getString("errorCode"), "验证errorCode");
			DataUtil.verifyEquals("hunterId:推手id不能为空", manageFeeStatisticsCall
					.getData().getString("errorMessage"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionStatistics_004() {
		
		Logger.start(true, "年份小于2018");
		
		try {

			manageFeeStatisticsReq.setHunterId(1022);
			manageFeeStatisticsReq.setYear(2017);

			manageFeeStatisticsCall.setData(manageFeeStatisticsReq);
			manageFeeStatisticsCall.callService();

			DataUtil.verifyEquals(400, manageFeeStatisticsCall.getJSONData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(100, manageFeeStatisticsCall.getJSONData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("year:年份不能小于2018年", manageFeeStatisticsCall
					.getJSONData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, manageFeeStatisticsCall.getData()
					.getBoolean("success"), "验证success");
			DataUtil.verifyEquals("ILLEGAL_ARGUMENT", manageFeeStatisticsCall
					.getData().getString("errorCode"), "验证errorCode");
			DataUtil.verifyEquals("year:年份不能小于2018年", manageFeeStatisticsCall
					.getData().getString("errorMessage"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
